package com.zzyk.web.controller;

import com.zzyk.main.configuration.aspectj.annotation.OperaLog;
import com.zzyk.main.configuration.exception.DeviceException;
import com.zzyk.main.configuration.exception.UserException;

import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.Company;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wss_PC
 * @DESCRIPTION
 * @create 2020-6-16 11:15
 */
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController {

    @Autowired
    private CompanyService companyService;


    /**
     * 根据ID删除企业信息
     *
     * @param id
     * @return
     */
    @OperaLog("删除企业信息")
    @GetMapping("/deleteInfoById")
    public Message deleteInfoById(Integer id) {
        try {
            companyService.deleteByPrimaryKey(id);
            return Message.success();
        } catch (DeviceException deviceException){
            deviceException.printStackTrace();
            return  Message.failed("企业下有设备不能删除，请先删除设备！");
        }catch (UserException userException){
            userException.printStackTrace();
            return Message.failed("请先删除该企业的用户信息！");
        }catch (Exception e) {
            e.printStackTrace();
            return Message.failed("删除失败");
        }
    }

    /**
     * 根据ID查询企业信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getInfoById")
    public Company getInfoById(Integer id) {
        return companyService.selectByPrimaryKey(id);
    }

    /**
     * 统计企业数量
     *
     * @return
     */
    @GetMapping("/countEnterprise")
    public Message countEnterprise() throws UserException {
        SysUser user = currentUser();
        String[] areaArr = user.getArea();
        try {
            return Message.success().addData("data",(companyService.getTotal(areaArr)));
        } catch (Exception e) {
            e.printStackTrace();
            return Message.failed("查询失败");
        }
    }

    /**
     * 查询企业信息
     *
     * @param param
     * @return
     */
    @GetMapping("/findEnterpriseS")
    public Message findEnterpriseS(String param) throws UserException {
        SysUser user = currentUser();
        String[] areas = user.getArea();
        try {
            return Message.success().addData("data",companyService.selectEnterpriseByParam(param, areas));
        } catch (Exception e) {
            e.printStackTrace();
           return Message.failed("加载企业信息失败");
        }
    }
}